Back to course home
0% completed
Vote For New Content
Nth Magical Number (hard)
Problem Statement
Given the three integers n, a, and b, return the nth magical number modulo
A positive integer is called magical if it is divisible by either a or b.
Examples
-
Example 1:
- Input:
n = 3, a = 2, b = 3 - Expected Output: 4
- Justification: The first three magical numbers divisible by 2 or 3 are 2, 3, and 4. The third magical number is 4.
- Input:
-
Example 2:
- Input:
n = 5, a = 3, b = 4 - Expected Output: 9
- Justification: The sequence of magical numbers that are divisible by 3 or 4 begins as 3, 4, 6, 8, 9. The fifth one in this sequence is 9.
- Input:
-
Example 3:
- Input:
n = 20, a = 3, b = 5 - Expected Output: 42
- Justification: The 20th magical number for
a = 30, andb = 5is 42.
- Input:
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Reverse Pairs
Solution: Nth Magical Number
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself